#!/usr/bin/env python
"""快速启动脚本"""

import os
import sys
from pathlib import Path

# 添加app目录到Python路径
current_dir = Path(__file__).parent
app_dir = current_dir / "app"
sys.path.insert(0, str(current_dir))

def check_dependencies():
    """检查依赖是否安装"""
    # required_packages = [
    #     "fastapi", "uvicorn", "pydantic", "python-jose", 
    #     "passlib", "python-dotenv", "httpx", "aiofiles"
    # ]
    
    # missing_packages = []
    # for package in required_packages:
    #     try:
    #         __import__(package.replace("-", "_"))
    #     except ImportError:
    #         missing_packages.append(package)
    
    # if missing_packages:
    #     print(f"❌ 缺少以下依赖包: {', '.join(missing_packages)}")
    #     print("请运行: pip install -r requirements.txt")
    #     return False
    
    print("✅ 所有依赖包已安装")
    return True

def check_config():
    """检查配置文件"""
    env_file = current_dir / ".env"
    env_example = current_dir / ".env.example"
    
    if not env_file.exists():
        if env_example.exists():
            print("⚠️  .env文件不存在，请复制.env.example并修改配置")
            print("运行: cp .env.example .env")
        else:
            print("⚠️  配置文件不存在，请创建.env文件")
        return False
    
    print("✅ 配置文件存在")
    return True

def main():
    """主函数"""
    print("🚀 启动文本检测API服务...")
    print("=" * 50)
    
    # 检查依赖
    if not check_dependencies():
        sys.exit(1)
    
    # 检查配置
    if not check_config():
        print("⚠️  配置检查失败，但仍可以运行（使用默认配置）")
    
    print("=" * 50)
    print("📝 服务信息:")
    print("   - 服务地址: http://localhost:8000")
    print("   - API文档: http://localhost:8000/docs")
    print("   - 健康检查: http://localhost:8000/health")
    print("=" * 50)
    
    # 导入并运行应用
    try:
        import uvicorn
        from app.main import app
        
        uvicorn.run(
            "app.main:app",
            host="0.0.0.0",
            port=8000,
            reload=True,
            log_level="info"
        )
    except KeyboardInterrupt:
        print("\n🛑 服务已停止")
    except Exception as e:
        print(f"❌ 启动失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main() 